/**
 * Project: Daycare Web Application
 * Package: com.made2ordersoftware.daycare.service
 * File: LoginDao.java
 * Created: Dec 4, 2010
 */

/*
 *  Copyright (c) 2010 Made 2 Order Software, LLC
 *
 *  All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 *  transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 *  You may not distribute the URL or redistribute the files to third parties. You may transfer the files
 *  onto a single storage device so long as you can prove ownership of the software. You may not reverse
 *  engineer, decompile, or disassemble the software or media files. You may not modify or translate the
 *  software or media, or distribute copies of the software or media without the written consent of Made
 *  2 Order Software
 */
package com.made2ordersoftware.daycare.service;

import com.made2ordersoftware.daycare.domain.AuthenticationEntity;
import com.made2ordersoftware.daycare.domain.PersonEntity;
import com.made2ordersoftware.daycare.exception.UnauthorizedUserException;
import com.made2ordersoftware.exceptions.DAOException;
import com.made2ordersoftware.service.AbstractDao;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Data Access Object for all login activities of the system.
 *
 * @author David Canning <dcannin@gmail.com>
 * @version 1.0.0
 */
@Stateless
public class AuthenticationDao extends AbstractDao<AuthenticationEntity, Long> {

    private static final Logger LOG = LoggerFactory.getLogger(AuthenticationDao.class);
    private static final long serialVersionUID = 1L;
    @PersistenceContext(unitName = "daycare_pu")
    private EntityManager em;

    /**
     * Creates an instance of <code>AuthenticationDao</code>.
     *
     */
    public AuthenticationDao() {
        super(AuthenticationEntity.class);
    }

    /**
     * Verifies user name and password match a person in system.
     *
     * @param userName User name of person being authenticated.
     * @param password Password of person being authenticated.
     *
     * @return The Person object
     *
     * @throws DAOException              Any exception not specifically caught in DAO layer.
     * @throws UnauthorizedUserException Thrown if user name or password does not match.
     */
    public PersonEntity authenticatePerson(String userName, String password) throws DAOException,
            UnauthorizedUserException {
        LOG.info("Start authenticatePerson for user name {}", userName);
        Query sql = em.createNamedQuery("Authentication.authenticationUser");
        sql.setParameter("userName", userName);
        sql.setParameter("password", password);
        try {
            AuthenticationEntity auth = (AuthenticationEntity) sql.getSingleResult();
            LOG.info("Finish authenticatePerson ...");
            return auth.getPerson();
        } catch (NoResultException e) {
            throw new UnauthorizedUserException(
                    "com.made2orderosftware.daycare.service:AuthenticationDao:authenticatePerson:"
                    + e.getCause() + ":Invalid user name or password");
        } catch (Exception e) {
            throw new DAOException(
                    "com.made2ordersoftware.daycare.service:AuthnticationDao:authenticatePerson:"
                    + e.getCause() + ":Unexpected server error");
        }
    }

    /**
     * Returns the EntityManager for the dao.
     *
     * @return the em
     */
    @Override
    protected EntityManager getEntityManager() {
        LOG.info("Getting entity manager....");
        return em;
    }
}
